# -*- coding: utf-8 -*-
from match import Extractor
import os.path
import regex as re


# 毒品持有的量刑与重量有关

def get_weight(line, keyword):
    line = line.replace('冰毒', '甲基苯丙胺')
    searchObj = re.findall(r'{}.*?(\d+\.\d+)[克g]|{}.*?(\d+)[克g]'.format(keyword, keyword), line)
    searchObj = set(searchObj)
    res = []
    for i in searchObj:
        for j in i:
            if j and float(j) not in res:
                res.append(float(j))
    sum_res = 0
    if res:
        sum_res = sum(res)
    return sum_res


class FeiFaChiYouDuPingExtractor(Extractor):
    """
        （一）鸦片二百克以上、海洛因、可卡因或者甲基苯丙胺十克以上；
        （二）二亚甲基双氧安非他明（MDMA）等苯丙胺类毒品（甲基苯丙胺除外）、吗啡二十克以上；
        （三）度冷丁（杜冷丁）五十克以上（针剂100“g/支规格的五百支以上，50“g/支规格的一千支以上；片剂25“g/片规格的二千片以上，50“g/片规格的一千片以上）；
        （四）盐酸二氢埃托啡二毫克以上（针剂或者片剂20“g/支、片规格的一百支、片以上）；
        （五）氯胺酮、美沙酮二百克以上；
        （六）三唑仑、安眠酮十千克以上；
        （七）咖啡因五十千克以上；
        （八）氯氮卓、艾司唑仑、地西泮、溴西泮一百千克以上；
        （九）大麻油一千克以上，大麻脂二千克以上，大麻叶及大麻烟三十千克以上；
        （十）罂粟壳五十千克以上；
    """

    @staticmethod
    def is_my_job(file_path):
        return "非法持有毒品" in file_path

    @staticmethod
    def extract(text):
        result = {}
        result['甲基苯丙胺(冰毒)'] = get_weight(text, '甲基苯丙胺')
        result['海洛因'] = get_weight(text, '海洛因')
        result['氯胺酮'] = get_weight(text, '氯胺酮')
        result['咖啡因'] = get_weight(text, '咖啡因')
        return result


if __name__ == '__main__':
    from match import *
    from storage import *
    import json
    import os

    data_util = DataUtil("E:/project/yaosu_law/pro_yaosu/非法持有毒品罪")
    extractors = [FeiFaChiYouDuPingExtractor]

    file_path, text = data_util.next_line()
    count_dict = {}
    count_key_list = ["甲基苯丙胺(冰毒)", "海洛因", "氯胺酮", "咖啡因"]
    while text:
        text_json = json.loads(text)
        if file_path not in count_dict:
            count_dict[file_path] = {"count": 0}
        for extractor in extractors:
            if extractor.is_my_job(file_path):
                result = extractor.extract("\n".join(text_json["content"]))
                for key in count_key_list:
                    result_value = result[key]
                    if (type(result_value) == int or type(result_value) == float):
                        if key in count_dict[file_path]:
                            count_dict[file_path][key] = count_dict[file_path][key] + result_value
                        else:
                            count_dict[file_path][key] = result_value
                    else:
                        if key in count_dict[file_path]:
                            if result_value in count_dict[file_path][key]:
                                count_dict[file_path][key][result_value] = count_dict[file_path][key][result_value] + 1
                            else:
                                count_dict[file_path][key][result_value] = 1
                        else:
                            count_dict[file_path][key] = {}
                            count_dict[file_path][key][result_value] = 1
        count_dict[file_path]["count"] = count_dict[file_path]["count"] + 1
        file_path, text = data_util.next_line()
    for key in count_dict:
        print(key)
        count = count_dict[key]["count"]
        for ckey in count_key_list:
            ckey_result = count_dict[key][ckey]
            if (type(ckey_result) == int or type(ckey_result) == float):
                print(str(ckey) + ":%f" % (ckey_result / float(count)))
            else:
                for cckey in ckey_result:
                    print(str(ckey) + "_" + str(cckey) + ":%f" % (ckey_result[cckey] / float(count)))
    print(count_dict)
